package com.mamahao.vmnetframework.framework.viewmodel

import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentActivity
import com.mamahao.vmnetframework.framework.event.BaseActionEvent
import com.mamahao.vmnetframework.framework.view.initViewModel

object LViewModelProviders{

    @JvmStatic
    fun <T : BaseViewModel> of(activity: FragmentActivity, modelClass: Class<T>, block: ((BaseActionEvent?) -> Boolean)? = null): T {
        val t = ViewModelProviders.of(activity).get(modelClass)
        activity.initViewModel(t, block)
        t.lifecycleOwner = activity
        return t
    }

    @JvmStatic
    fun <T : BaseViewModel> of(fragment: Fragment, modelClass: Class<T>, block: ((BaseActionEvent?) -> Boolean)? = null): T {
        val t = ViewModelProviders.of(fragment).get(modelClass)
        fragment.initViewModel(t, block)
        t.lifecycleOwner = fragment
        return t
    }
}